package com.manager.config.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.manager.common.result.Result;
import com.manager.config.configEntity.Config;
import com.manager.config.mapper.ConfigTypeMapper;
import com.manager.config.service.*;
import com.manager.model.*;

import com.manager.model.model.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/set")
@Api(tags= "配置管理")
@CrossOrigin(origins = "*")
public class ConfigController {

    private ConfigTypeMapper typeMapper;
    @Autowired
    private AgentService agentService;
    @Autowired
    private CountryService countryService;
    @Autowired
    private LevelService levelService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ConfigTypeService typeService;

    @GetMapping("/get")
    @ApiOperation("下拉显示配置信息")
    public Result getSettings(@RequestParam Integer flag){
        List configList = null;
        switch (flag){
            case 0:
                QueryWrapper<CustomerType> wrapper0 = new QueryWrapper<>();
                wrapper0.eq("status",1).select("id","name");
                configList = typeService.list(wrapper0);
                List<Config> types = new ArrayList<>();
                for(int i = 0; i < configList.size();i++){
                        types.add(new Config(((CustomerType)configList.get(i)).getId(),((CustomerType)configList.get(i)).getName()));
                }
                return Result.ok(types);
            case 1:
                QueryWrapper<Level> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("status",1).select("id","level");
                configList = levelService.list(wrapper1);
                List<Config> levels = new ArrayList<>();
                for(int i = 0; i < configList.size();i++){
                    levels.add(new Config(((Level)configList.get(i)).getId(),((Level)configList.get(i)).getLevel()));
                }
                return Result.ok(levels);
            case 2:
                QueryWrapper<Country> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("status",1).select("id","name");
                configList = countryService.list(wrapper2);
                List<Config> countrys = new ArrayList<>();
                for(int i = 0; i < configList.size();i++){
                    countrys.add(new Config(((Country)configList.get(i)).getId(),((Country)configList.get(i)).getName()));
                }
                return Result.ok(countrys);
            case 3:
                QueryWrapper<Project> wrapper3 = new QueryWrapper<>();
                wrapper3.eq("status",1).select("id","name");
                configList = projectService.list(wrapper3);
                List<Config> projects = new ArrayList<>();
                for(int i = 0; i < configList.size();i++){
                    projects.add(new Config(((Project)configList.get(i)).getId(),((Project)configList.get(i)).getName()));
                }
                return Result.ok(projects);
            case 4:
                QueryWrapper<Agent> wrapper4 = new QueryWrapper<>();
                wrapper4.eq("status",1).select("id","name");
                configList = agentService.list(wrapper4);
                List<Config> agents = new ArrayList<>();
                for(int i = 0; i < configList.size();i++){
                    agents.add(new Config(((Agent)configList.get(i)).getId(),((Agent)configList.get(i)).getName()));
                }
                return Result.ok(agents);
            default:
                return Result.fail("false");
        }
    }


    @PostMapping("/add")
    @ApiOperation("添加配置")
    public Result addSetting(@RequestParam Integer flag , @RequestParam String content){
        switch (flag){
            case 0:
                if(typeService.addType(content)){
                    return Result.ok("true");
                }
                break;
            case 1:
                if(levelService.addLevel(content)){
                    return Result.ok("true");
                }
                break;
            case 2:
                if(countryService.addCountry(content)){
                    return Result.ok("true");
                }
                break;
            case 3:
                if (projectService.addProject(content)) {
                    return Result.ok("true");
                }
                break;
            case 4:
                if(agentService.addAgent(content)){
                    return Result.ok("true");
                }
                break;
        }
        return Result.ok("false");
    }

    @PostMapping("/disable")
    @ApiOperation("禁用配置")
    public Result disableSetting(@RequestParam Integer flag, @RequestParam Integer[] setIds){
        for(int i = 0; i < setIds.length ;i++){
            switch (flag){
                case 0:
                    CustomerType type = typeService.getById(setIds[i]);
                    type.setStatus(0);
                    typeService.updateById(type);
                    break;
                case 1:
                    Level level = levelService.getById(setIds[i]);
                    level.setStatus(0);
                    levelService.updateById(level);
                    break;
                case 2:
                    Country country = countryService.getById(setIds[i]);
                    country.setStatus(0);
                    countryService.updateById(country);
                    break;
                case 3:
                    Project project = projectService.getById(setIds[i]);
                    project.setStatus(0);
                    projectService.updateById(project);
                    break;
                case 4:
                    Agent agent = agentService.getById(setIds[i]);
                    agent.setStatus(0);
                    agentService.save(agent);
                    break;
            }
        }
        return Result.ok("true");
    }
}
